﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using HZH_Controls.Controls;

namespace HotelManager
{
    public partial class Form_Main : Skin_DevExpress
    {
        public Form_Main()
        {
            InitializeComponent();
        }

        private void Main_Form_Load(object sender, EventArgs e)
        {
            Views.Form_Login f_Login = new Views.Form_Login();
            f_Login.ShowDialog();
            if (!f_Login.isLoginSuccessful)
            {
                Application.Exit();
            }
            opLabel.Text = Utils.GlobalData.LoginUser.UserName;

            LoadRoomStatus();
        }

        private void LoadRoomStatus()
        {
            //加载房间类型到横向列表
            DataView roomTypes = DB.GetDs("select 类型ID,客房类型 from 房间类型表").Tables[0].DefaultView;
            List<KeyValuePair<string, string>> lstHL = new List<KeyValuePair<string, string>>();
            lstHL.Add(new KeyValuePair<string, string>("0", "全部"));
            for (int i = 1; i < roomTypes.Count + 1; i++)
            {
                lstHL.Add(new KeyValuePair<string, string>(roomTypes[i - 1][0].ToString(), roomTypes[i - 1][1].ToString()));
            }
            ucHorizontalList1.DataSource = lstHL;

            //加载房间类型状态表
            string roomStatusSql = @"
            SELECT
	            房间类型表.客房类型,
	            SUM(CASE WHEN 房间状态表.是否空 = '是' THEN 1 ELSE 0 END) AS 空房数,
	            COUNT(1) AS 总房间数
            FROM
	            房间状态表,房间类型表
            WHERE 房间状态表.类型ID = 房间类型表.类型ID
            GROUP BY
	            房间类型表.客房类型";
            DataView rSS_dv = DB.GetDs(roomStatusSql).Tables[0].DefaultView;
            roomStatusList.BeginUpdate();
            roomStatusList.Items.Clear();
            for (int i = 0; i < rSS_dv.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.Text = rSS_dv[i][0].ToString();
                item.SubItems.Add(rSS_dv[i][1].ToString() + " / " + rSS_dv[i][2].ToString());
                roomStatusList.Items.Add(item);
            }
            roomStatusList.EndUpdate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timeLabel.Text = DateTime.Now.ToString();
        }

        private void LoadRoomList(string RoomTypeID)
        {
            string sql = string.Empty;
            if (RoomTypeID == "0")//加载全部房型
            {
                sql = "select * from 房间状态表";
            }
            else
            {
                sql = "select * from 房间状态表 where 类型ID='" + RoomTypeID + "'";
            }
            DataView rooms = DB.GetDs(sql).Tables[0].DefaultView;
            string realID = (Convert.ToInt32(RoomTypeID) - 1).ToString();
            roomList.BeginUpdate();
            roomList.Clear();
            for (int i = 0; i < rooms.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.Text = rooms[i]["房间号"].ToString();
                // 0 - empty  1 - ordered
                // 2 - full   3 - stop
                if (rooms[i]["是否停用"].ToString() == "是")
                {
                    item.ImageIndex = 3;
                }
                else if (rooms[i]["是否预订"].ToString() == "是")
                {
                    item.ImageIndex = 1;
                }
                else if (rooms[i]["是否空"].ToString() == "是")
                {
                    item.ImageIndex = 0;
                }
                else if (rooms[i]["是否空"].ToString() == "否")
                {
                    item.ImageIndex = 2;
                }
                roomList.Items.Add(item);
            }
            roomList.LargeImageList = imageList1;
            roomList.EndUpdate();
        }

        private void ucHorizontalList1_SelectedItemEvent(object sender, EventArgs e)
        {
            UCHorizontalListItem item = ucHorizontalList1.SelectedItem;
            LoadRoomList(item.DataSource.Key);
        }
        private void roomList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (roomList.SelectedItems.Count > 0)
            {
                skinLabel1.Text =
                    DB.GetData(@"select 客房类型 from 房间类型表,房间状态表 
                            where 房间类型表.类型ID  = 房间状态表.类型ID and 房间状态表.房间号="
                    + roomList.SelectedItems[0].Text).ToString() + " " + roomList.SelectedItems[0].Text;

            }
        }
        private void roomList_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && roomList.SelectedItems.Count > 0)
            {
                skinContextMenuStrip1.Show(roomList, e.Location);
            }
        }

        private void skinContextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            int RoomStatus = roomList.SelectedItems[0].ImageIndex;
            switch (RoomStatus)
            {
                // 0 - empty  1 - ordered
                // 2 - full   3 - stop
                case 0:
                    入住ToolStripMenuItem.Enabled = true;
                    预订ToolStripMenuItem.Enabled = true;
                    退房ToolStripMenuItem.Enabled = false;
                    暂停使用ToolStripMenuItem.Enabled = true;
                    暂停使用ToolStripMenuItem.Text = "暂停使用";
                    break;
                case 1:
                    入住ToolStripMenuItem.Enabled = true;
                    预订ToolStripMenuItem.Enabled = false;
                    退房ToolStripMenuItem.Enabled = false;
                    暂停使用ToolStripMenuItem.Enabled = false;
                    暂停使用ToolStripMenuItem.Text = "暂停使用";
                    break;
                case 2:
                    入住ToolStripMenuItem.Enabled = false;
                    预订ToolStripMenuItem.Enabled = false;
                    退房ToolStripMenuItem.Enabled = true;
                    暂停使用ToolStripMenuItem.Enabled = false;
                    暂停使用ToolStripMenuItem.Text = "暂停使用";
                    break;
                case 3:
                    入住ToolStripMenuItem.Enabled = false;
                    预订ToolStripMenuItem.Enabled = false;
                    退房ToolStripMenuItem.Enabled = false;
                    暂停使用ToolStripMenuItem.Enabled = true;
                    暂停使用ToolStripMenuItem.Text = "开始使用";
                    break;
                default:
                    break;
            }
        }

        //入住登记
        private void 入住ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Views.Form_CheckIn f_CheckIn = new Views.Form_CheckIn();
            f_CheckIn.RoomNum = roomList.SelectedItems[0].Text;
            f_CheckIn.ShowDialog();
            ucHorizontalList1_SelectedItemEvent(sender, e);
        }
        private void skinButton2_Click(object sender, EventArgs e)
        {
            Views.Form_CheckIn f_CheckIn = new Views.Form_CheckIn();
            f_CheckIn.ShowDialog();
            ucHorizontalList1_SelectedItemEvent(sender, e);
        }
        //预订登记
        private void skinButton1_Click(object sender, EventArgs e)
        {
            Views.Form_Order f_Order = new Views.Form_Order();
            f_Order.ShowDialog();
        }

        private void 预订ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Views.Form_Order f_Order = new Views.Form_Order();
            f_Order.RoomNum = roomList.SelectedItems[0].Text;
            f_Order.ShowDialog();
            ucHorizontalList1_SelectedItemEvent(sender, e);
        }

        private void 暂停使用ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (暂停使用ToolStripMenuItem.Text == "暂停使用")
            {
                DialogResult dr = MessageBox.Show("确实要暂停这个房间的使用吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    DB.Execute("update 房间状态表 set 是否停用='是' where 房间号=" + roomList.SelectedItems[0].Text);
                    ucHorizontalList1_SelectedItemEvent(sender, e);
                }
            }
            else if (暂停使用ToolStripMenuItem.Text == "开始使用")
            {
                DialogResult dr = MessageBox.Show("确实要启用这个房间的使用吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    DB.Execute("update 房间状态表 set 是否停用='否' where 房间号=" + roomList.SelectedItems[0].Text);
                    ucHorizontalList1_SelectedItemEvent(sender, e);
                }
            }
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            new Views.Form_RoomManage().ShowDialog();
            LoadRoomStatus();
        }

        private void skinButton6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
